﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FolderTransfer
{
    public partial class FormMain : Form
    {
        private Server server;
        private Client client;
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnServer_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string btnText = btn.Text;
            if (btnText == "Open")
            {
                server = new Server(this);
                btn.Text = "Close";
                server.Listen();
                tbServerIp.Text = server.Ip.ToString();
                tbServerPort.Text = server.Port.ToString();
                btnPathSelect.Enabled = true;
            }
            else
            {
                btn.Text = "Open";
                server.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string btnText = btn.Text;
            if (btnText == "Connect")
            {
                btn.Text = "Break";
                String ip = tbClientIp.Text;
                int port = int.Parse(tbClientPort.Text);
                client = new Client(ip, port, this);
                client.Connect();
            }
            else
            {
                btn.Text = "Connect";
                server.Close();
            }
        }

        private void btnPathSelect_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.tbFolderPath.Text = folderBrowserDialog.SelectedPath;
                btnSend.Enabled = true;
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (server.FolderClient == null)
            {
                return;
            }
            String folderPath = tbFolderPath.Text;
            if (folderPath.Trim() == "")
            {
                return;
            }
            server.Send(folderPath);
            btnSend.Enabled = false;
            btnPathSelect.Enabled = false;
        }
        private delegate void SetBtnEnabledDelegate(string btnName, bool value);
        public void SetBtnEnabled(string btnName, bool value)
        {
            if (btnName == "btnReceive")
            {
                if (btnReceive.InvokeRequired)
                {
                    SetBtnEnabledDelegate d = SetBtnEnabled;
                    this.Invoke(d, btnName, value);
                }
                else
                {
                    btnReceive.Enabled = value;
                }
            }
            else if (btnName == "btnSend")
            {
                if (btnSend.InvokeRequired)
                {
                    SetBtnEnabledDelegate d = SetBtnEnabled;
                    this.Invoke(d, btnName, value);
                }
                else
                {
                    btnSend.Enabled = value;
                }
            }
            else if (btnName == "btnRecv")
            {
                if (btnRecv.InvokeRequired)
                {
                    SetBtnEnabledDelegate d = SetBtnEnabled;
                    this.Invoke(d, btnName, value);
                }
                else
                {
                    btnRecv.Enabled = value;
                }
            }
            else if (btnName == "btnPathSelect")
            {
                if (btnPathSelect.InvokeRequired)
                {
                    SetBtnEnabledDelegate d = SetBtnEnabled;
                    this.Invoke(d, btnName, value);
                }
                else
                {
                    btnPathSelect.Enabled = value;
                }
            }
        }
        private delegate void SetLabelDelegate(string labelName, string txt);
        public void SetLable(string labelName, string txt)
        {
            if (labelName == "tbStatus")
            {
                if (tbStatus.InvokeRequired)
                {
                    this.Invoke(new SetLabelDelegate(SetLable), labelName, txt);
                }
                else
                {
                    tbStatus.Text = txt;
                }
            }
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {
            if (tbRecvPath.Text.Trim() == "")
            {
                return;
            }
            client.RecvPath = tbRecvPath.Text;
            client.FolderClient.SendString("Agree");
            btnReceive.Enabled = false;
            btnRecv.Enabled = false;
        }

        private void btnRecv_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.tbRecvPath.Text = folderBrowserDialog.SelectedPath;
                btnReceive.Enabled = true;
            }
        }

        private void FormMain_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileNames = null;
            if (e.Data.GetDataPresent("FileNameW"))//判断拖放的对象中是不是有FileNameW格式的文件名
            {
                fileNames = (string[])e.Data.GetData("FileNameW");
            }

            if (fileNames.Length > 0)
            {
                DirectoryInfo dir = new DirectoryInfo(fileNames[0]);
                if (dir.Exists)
                {
                    if (btnPathSelect.Enabled)
                    {
                        tbFolderPath.Text = fileNames[0];
                        btnSend.Enabled = true;
                        btnPathSelect.Enabled = false;
                        return;
                    }
                    if (btnRecv.Enabled)
                    {
                        tbRecvPath.Text = fileNames[0];
                        btnReceive.Enabled = true;
                        btnRecv.Enabled = false;
                        return;
                    }
                }
            }
        }

        private void FormMain_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("FileNameW"))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
    }
}
